package com.cheejee.goodscenter.service;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import javax.annotation.Resource;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import com.cheejee.goodscenter.module.entity.Brand;
import com.cheejee.goodscenter.service.exception.CheckException;
import com.cheejee.goodscenter.service.exception.DuplicateException;

/**
 * @author CARRY
 * @Date 2020年10月23日下午3:57:26
 */
@SpringBootTest
class BrandServiceTeset {

	@Resource
	private BrandService bs;

	/**
	 * Test method for
	 * {@link com.cheejee.goodscenter.service.impl.BrandServiceImpl#queryBrandByInitial(java.lang.String, int, int)}.
	 */
	@Test
	void testQueryBrand() {
		assertThat(bs.queryBrand("xiaomi", null, 1, 11)).hasSize(1);
		assertThat(bs.queryBrand(null, "x", 0, 0)).hasSize(1);
		assertThat(bs.queryBrand("xiaomi", "x", 0, 0)).hasSize(1);
	}

	/**
	 * Test method for
	 * {@link com.cheejee.goodscenter.service.impl.BrandServiceImpl#addBrand(com.cheejee.goodscenter.module.entity.Brand)}.
	 */
	@Test
	void testAddAndDeleteAndChangeBrand() {
		
		assertThrows(CheckException.class, () -> bs.addBrand(null));
		
		// add
		Brand b = new Brand();
		b.setInitial("a");
		b.setName("a");
		bs.addBrand(b);

		assertNotNull(bs.queryBrandById(b.getId()));
		assertThrows(DuplicateException.class, () -> bs.addBrand(b));

		// change
		b.setDescribe("d");
		bs.changeBrand(b);
		assertEquals("d", bs.queryBrandById(b.getId())
				.getDescribe());
		b.setName("华为");
		assertThrows(DuplicateException.class, () -> bs.changeBrand(b));

		// delete
		bs.deleteBrand(b.getId());
		assertNull(bs.queryBrandById(b.getId()));
	}

}
